<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="searchContent">
        <el-input
          v-model="queryParams.searchContent"
          placeholder="请输入您想搜索的内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="releaseDate">
        <el-date-picker clearable
                        v-model="queryParams.releaseDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完结时间" prop="endDate">
        <el-date-picker clearable
                        v-model="queryParams.endDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择完结时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery2">es搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['animation:animation:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maidList">
      <el-table-column label="姓名" align="center" prop="name">
        <template slot-scope="scope">
          <span v-html="scope.row.name"></span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="picture">
        <template slot-scope="scope">
          <el-image
            style="width: 100px;height: 100px"
            :src="scope.row.picture == null ? coverSuffix + '/profile/upload/2024/08/02/f74ec417-132c-40bd-bcf2-c8c9370aa968.jpg' : coverSuffix + scope.row.picture"
            fit="fill"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="性别" align="center" prop="sex"/>
      <el-table-column label="出处" align="center" prop="source">
        <template slot-scope="scope">
          <span v-html="scope.row.source"></span>
        </template>
      </el-table-column>
      <el-table-column label="配音" align="center" prop="dubbing">
        <template slot-scope="scope">
          <span v-html="scope.row.dubbing"></span>
        </template>
      </el-table-column>
      <el-table-column label="喜欢的人" align="center" prop="likedPerson"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['animation:animation:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['animation:animation:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改女仆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="resetCover">
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="图片" prop="picture">
          <el-upload
            action="http://localhost:8080/fileUpload/upload"
            :headers=uploadHeader
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :file-list="coverObj"
            :on-success="handleSuccess">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>

        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable
                          v-model="form.birthday"
                          type="date"
                          format="MM-dd"
                          value-format="MM-dd"
                          placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"/>
        </el-form-item>
        <el-form-item label="请选择性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出处" prop="source">
          <el-input v-model="form.source" placeholder="请输入出处"/>
        </el-form-item>
        <el-form-item label="配音" prop="dubbing">
          <el-input v-model="form.dubbing" placeholder="请输入配音"/>
        </el-form-item>
        <el-form-item label="喜欢的人" prop="likedPerson">
          <el-input v-model="form.likedPerson" placeholder="请输入喜欢的人"/>
        </el-form-item>
        <el-form-item label="角色经历" prop="experience">
          <el-input type="textarea" :rows="4" v-model="form.experience" placeholder="请输入角色经历"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnimation,
  getAnimation,
  delAnimation,
  addAnimation,
  updateAnimation,
  listAnimationByEs
} from "@/api/animation/animation";
import {listMaid} from "@/api/maid/maid";
import {parseTime} from "../../utils/ruoyi";

export default {
  name: "Maid",
  data() {
    return {
      // 封面前缀
      coverSuffix: 'http://localhost:82/',
      // 上传文件对象
      coverObj: [
        // {
        //   name: '',
        //   url: '',
        // }
      ],
      // 上传文件请求头部
      uploadHeader: {
        Authorization: ""
      },
      dialogImageUrl: '',
      dialogVisible: false,
      // 番剧状态选项
      options: [
        {
        value: '0',
        label: '未知'
      }, {
        value: '1',
        label: '男'
      }, {
        value: '2',
        label: '女'
      }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 女仆表格数据
      maidList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchContent: null,
        releaseDate: null,
        endDate: null,
        type: null,
        episodes: null,
        status: null,
        intro: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入番剧名", trigger: "blur" }
        ],
        releaseDate: [
          { required: true, message: "请选择发布时间", trigger: "change" }
        ],
        type: [
          { required: true, message: "请输入番剧类型", trigger: "blur" }
        ],
        episodes: [
          { required: true, message: "请输入总集数", trigger: "blur" }
        ],
        status: [
          { required: true, message: "请选择状态", trigger: "change" }
        ],
        intro: [
          { required: true, message: "请输入简介", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    transferEndDate(date) {
      // 转换完结时间
      if (date == null) {
        return '未知'
      } else {
        return date
      }
    },
    resetCover() {
      this.coverObj = []
    },
    handleSuccess(response, file, fileList) {
      // 将上传成功的对象赋值给form
      this.form.cover = response.fileId;
      this.form.actualUrl = response.relativePath;
      // console.log(this.coverObj)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
      console.log(this.dialogImageUrl)
    },
    // 获取番剧状态
    getStatus(scope) {
      console.log(scope)
      if (status === '0') {
        return '未知'
      }
      if (status === '1') {
        return '已完结'
      }
      if (status === '2') {
        return '更新中'
      }
    },
    /** 查询番剧列表 */
    getList() {
      this.loading = true;
      this.reset();
      listMaid(this.queryParams).then(response => {
        this.animationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      // 获取并赋值token
      const token = this.$store.state.user.token;
      this.uploadHeader.Authorization = token
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        releaseDate: null,
        endDate: null,
        aType: null,
        episodes: null,
        status: null,
        intro: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      // this.getListByEs(this.queryParams.searchContent);
    },
    /** es搜索按钮操作 */
    handleQuery2() {
      this.queryParams.pageNum = 1;
      // this.getList();
      this.getListByEs(this.queryParams.searchContent);
    },
    // es查询
    getListByEs(text) {
      listAnimationByEs(text).then(response => {
        this.animationList = response;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增番剧";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.coverObj = [];
      const id = row.id || this.ids
      getAnimation(id).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改番剧";
        // 赋值封面
        if (response.cover!= null) {
          this.coverObj = [{
            name: "cover",
            url: this.coverSuffix + response.cover
          }]
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAnimation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAnimation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除番剧编号为"' + ids + '"的数据项？').then(function () {
        return delAnimation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('animation/animation/export', {
        ...this.queryParams
      }, `animation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
</style>

